Skip to content

Conversation

@lordaarush
Copy link

@lordaarush lordaarush commented Jan 17, 2026

Description

This PR fixes a failure in the Torch-TensorRT dynamo converter when lowering repeat() patterns that internally get rewritten into expand() under dynamic or symbolic shapes. In these situations, len(input_t.shape) may fail because symbolic dimensions are not standard Python sequences, leading to errors such as:

ValueError: len() should return >= 0

This was triggered by positional embedding patterns like:

x[..., None, None, :].repeat(batch, 1, H, W, 1)

Root cause:
After padding singleton dimensions, the converter asserted:
assert len(input_t.shape) == shape_rank
However, calling len() on symbolic shapes can fail or return invalid results, causing the converter to raise Python-level errors during lowering.

Fix:
Replaces the unsafe assert with a robust rank check that:

  • attempts to read the rank of input_t.shape safely,
  • falls back to the known initial_tensor_rank if len() is not supported, and
  • emits a clear RuntimeError if ranks still mismatch.

This preserves all existing expand() semantics. There are no behavior changes for static shapes.

A minimal regression test has been added at:
tests/dynamo/test_repeat_expand_repro.py
It reproduces the issue in #3972 and is skipped automatically when CUDA + Torch-TensorRT are unavailable.

Fixes: #3972

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code where needed
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally where applicable (test is skipped when Torch-TensorRT runtime is unavailable)
  • I have added the relevant labels to my PR so that reviewers are notified

@meta-cla meta-cla bot added the cla signed label Jan 17, 2026
@github-actions github-actions bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jan 17, 2026
@github-actions github-actions bot requested a review from zewenli98 January 17, 2026 17:27
Copy link
Collaborator

@apbose apbose Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I don't think the above fix addresses the issue. Since dynamic shapes are already handled in prepend_ones. Also current_rank should now be the shape it is expanded to.

try:
        current_rank = len(input_t.shape)
except Exception:
        current_rank = shape_rank

In the below test case there will be 0 computational nodes that will depend on runtime input, since the shape values will be constant. You could make them dynamic to invoke the converter.

I went through the above issue and looks like the root issue is dims being 10 here which is not permitted in TRT. It can handle till 8 max dims- https://docs.nvidia.com/deeplearning/tensorrt/latest/_static/c-api/classnvinfer1_1_1_dims64.html

Pytorch decomposes repeat to unsqueeze-> expand -> permute -> reshape

But for 5D tensor layer.reshape_dims = new_shape fails here, since DIMS can't be 10 here, and it fails in the dynamic case too in layer.set_input(1, reshape_dim_layer.get_output(0)). Hence input_tensor.shape would come invalid.

The original example would work with only 4 dimensions. Instead of emb_t = self.pos_emb_t[: pe_size[0]][None, :, None, None, :].repeat(batch_size, 1, pe_size[1], pe_size[2], 1) 5 dims here.

WAR would be to replace repeat with expand without broadcasting the dimension or using tile operation. I need to look into this more.

Ideally for tests below we would want the test case to be in https://github.com/pytorch/TensorRT/tree/main/tests/py/dynamo/conversion if it is a converter fix, so below location would not work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing this, looks like I misunderstood what was actually failing and ended up fixing the wrong thing. I see the issue much more clearly now. I’ll take another pass at it with the correct context and follow up if I find something useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug] Shape mismatch when using repeat

2 participants